//
//  PointGoodsViewController.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/29.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "PointGoodsViewController.h"

@interface PointGoodsViewController ()

@end

@implementation PointGoodsViewController{
    
    UIView *navView;

    PointGoodsModel *pointsGoodsModel;
    
    UIWebView *webView;
    UIScrollView *scrollView;
    UILabel *numberLabel;
    UIButton *button;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    
    [self getPointDetail];
    
}

- (void) getPointDetail {
    
    NSMutableDictionary * params = NSMutableDictionary.new;
    [params setValue:[NSString stringWithFormat:@"%d",self.id] forKey:@"id"];

    [HttpHelper request:@"point/detail" :@"GET" :params successBlock:^(NSDictionary *responseDic) {
        
        [GoodsModel mj_setupObjectClassInArray:^NSDictionary *{
            return @{@"images" : @"ImageModel"};
        }];
        
       self->pointsGoodsModel = [PointGoodsModel mj_objectWithKeyValues:responseDic[@"data"]];
        
         [self drawView];
    
    } failureBlock:^(NSError *error) {
    }];
    
}


- (void) drawView {
    
    self->navView = UIView.new;
    [self.view addSubview:navView];
    
    [navView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.top).offset(0);
        make.left.equalTo(self.view.left).offset(0);
        make.right.equalTo(self.view.right).offset(0);
        make.height.equalTo(64);
    }];
    
    
    UIView *divideLine = UIView.new;
    divideLine.backgroundColor =UIColorFromRGB(0xCCCCCC) ;
    [navView addSubview:divideLine];
    
    [divideLine makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->navView.bottom);
        make.left.equalTo(self.view.left).offset(0);
        make.right.equalTo(self.view.right).offset(0);
        make.height.equalTo(1/[[UIScreen mainScreen] scale]);
    }];
    
    
    UIImageView *backImage = UIImageView.new;
    backImage.image = [UIImage imageNamed:@"back"];
    [navView addSubview:backImage];
    
    [backImage makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->navView.left).offset(15);
        make.top.equalTo(self->navView.top).offset(33);
        make.width.equalTo(10);
        make.height.equalTo(18);
    }];
    
    
    UIButton *btn = UIButton.new;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:btn];
    
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->navView.left).offset(10);
        make.top.equalTo(self->navView.top).offset(20);
        make.width.equalTo(50);
        make.height.equalTo(40);
    }];
    
    
    UILabel *title = UILabel.new;
    title.textColor = [UIColor blackColor];
    title.font = [UIFont systemFontOfSize:20];
    title.text = @"商品详情";
    [navView addSubview:title];
    
    [title makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->navView.centerX);
        make.centerY.equalTo(backImage.centerY);
    }];

    
    UIView *bottomView = UIView.new;
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(0);
        make.right.equalTo(self.view.right).offset(0);
        make.bottom.equalTo(self.view.bottom).offset(0);
        make.height.equalTo(49);
    }];

    
    self->numberLabel = UILabel.new;
    self->numberLabel.textAlignment = NSTextAlignmentCenter;
    self->numberLabel.font = [UIFont systemFontOfSize:15.0];
    self->numberLabel.textColor = MAIN_COLOR;
    [bottomView addSubview:self->numberLabel];
    
    [self->numberLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomView.top);
        make.left.equalTo(bottomView.left).offset(10);
        make.bottom.equalTo(0);
    }];
    
    
    self->button = UIButton.new;
    self->button.layer.cornerRadius = 4;
    self->button.backgroundColor = MAIN_COLOR;
    self->button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [self->button setTitle:@"立刻兑换" forState:UIControlStateNormal];
    [self->button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self->button.titleLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:14];
    [bottomView addSubview:self->button];
    
    [self->button makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bottomView.right).offset(-10);
        make.height.equalTo(30);
        make.width.equalTo(110);
        make.centerY.equalTo(bottomView.centerY);
    }];
    
    //[self setBottomView];
    
    //分割线
    UIView *divideLine2 = UIView.new;
    divideLine2.backgroundColor = UIColorFromRGB(0xB2B2B2) ;
    [bottomView addSubview:divideLine2];
    
    [divideLine2 makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomView.top);
        make.left.equalTo(bottomView.left);
        make.right.equalTo(bottomView.right);
        make.height.equalTo(1/[[UIScreen mainScreen] scale]);
    }];

    
    self->webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - 49)];
    self->webView.delegate = self;
    self->webView.scrollView.scrollEnabled = NO;
    self->webView.backgroundColor = [UIColor whiteColor];
    self->webView.hidden = YES;
    [self.view addSubview:self->webView];
    
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    [self->webView loadHTMLString:self->pointsGoodsModel.goods.detail baseURL:baseURL];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //[LoadingView hidden:self.view];
    
    // 通过js获取webview的高
    float webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    
    UIView *contentView = UIView.new;
    contentView.backgroundColor = [UIColor whiteColor];
    
     SDCycleScrollView * cycleScrollView = SDCycleScrollView.new;
    cycleScrollView.autoScrollTimeInterval = 3;// 自定义轮播时间间隔
    
        [contentView addSubview:cycleScrollView];
    
    [cycleScrollView makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(0);
            make.height.equalTo(MAIN_SCREEN_WIDTH);
        }];
    
        NSMutableArray *sliderImgUrl = NSMutableArray.new;
        for(ImageModel *imageModel in self->pointsGoodsModel.goods.images){
            [sliderImgUrl addObject:imageModel.img_url];
        }
    
        cycleScrollView.imageURLStringsGroup = sliderImgUrl;
    
    UIView *lineView = UIView.new;
    lineView.backgroundColor = [UIColor grayColor];
    [contentView addSubview:lineView];
    
    [lineView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView.left).offset(0);
        make.right.equalTo(contentView.right).offset(0);
        make.bottom.equalTo(cycleScrollView.bottom).offset(0);
        make.height.equalTo(1/[[UIScreen mainScreen] scale]);
        
    }];
    
    UILabel *nameLabel = UILabel.new;
    nameLabel.text = self->pointsGoodsModel.name;
    nameLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:18];
    nameLabel.textColor = MAIN_FONT_COLOR;
    nameLabel.numberOfLines = 0;
    nameLabel.preferredMaxLayoutWidth = MAIN_SCREEN_WIDTH - 20;
    [contentView addSubview:nameLabel];
    
    [nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cycleScrollView.bottom).offset(10);
        make.left.equalTo(contentView.left).offset(10);
        make.right.equalTo(contentView.right).offset(-10);
    }];
    
    
    UILabel *pointLabel = UILabel.new;
    pointLabel.textAlignment = NSTextAlignmentCenter;
    pointLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:28];
    pointLabel.textColor = MAIN_COLOR;
    pointLabel.numberOfLines = 1;
    pointLabel.text = [NSString stringWithFormat:@"%d", self->pointsGoodsModel.points];
    [contentView addSubview:pointLabel];
    
    [pointLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.bottom).offset(15);
        make.centerX.equalTo(contentView.centerX);
    }];
    
    UILabel *pointDescLabel = UILabel.new;
    pointDescLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:12];
    pointDescLabel.textColor = UIColorFromRGB(0x999999);
    pointDescLabel.numberOfLines = 1;
    pointDescLabel.text = @"积分";
    [contentView addSubview:pointDescLabel];
    
    [pointDescLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pointLabel.centerY);
        make.left.equalTo(pointLabel.right).offset(5);
        //make.bottom.equalTo(_headerView).offset(-30);
    }];
    
    
    UILabel *marketPriceLabel = UILabel.new;
    marketPriceLabel.textAlignment = NSTextAlignmentCenter;
    marketPriceLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:12];
    marketPriceLabel.textColor = UIColorFromRGB(0x999999);
    marketPriceLabel.numberOfLines = 1;
    // 中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"市场价：%d", self->pointsGoodsModel.goods.price] attributes:attribtDic];
    marketPriceLabel.attributedText = attribtStr;
    [contentView addSubview:marketPriceLabel];
    
    [marketPriceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pointLabel.centerY);
        make.left.equalTo(pointDescLabel.right).offset(10);
    }];
    
    UIView *goodsDescView = UIView.new;
    goodsDescView.backgroundColor = BG_COLOR;
    [contentView addSubview:goodsDescView];
    
    [goodsDescView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pointLabel.bottom).offset(10);
        make.left.equalTo(contentView.left);
        make.right.equalTo(contentView.right);
        make.height.equalTo(36);
    }];
    
    UILabel *goodsDescLabel = UILabel.new;
    goodsDescLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:14];
    goodsDescLabel.textColor = SECOND_FONT_COLOR;
    goodsDescLabel.numberOfLines = 1;
    goodsDescLabel.text = @"商品描述";
    [contentView addSubview:goodsDescLabel];
    
    [goodsDescLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(goodsDescView.top).offset(0);
        make.left.equalTo(goodsDescView.left).offset(10);
        make.right.equalTo(goodsDescView.right);
        make.bottom.equalTo(goodsDescView.bottom);
    }];
    
    [self->webView removeFromSuperview];
    self->webView.hidden = NO;
    [contentView addSubview:webView];
    
    [webView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView.left).offset(0);
        make.right.equalTo(contentView.right).offset(0);
        make.top.equalTo(goodsDescLabel.bottom);
        make.height.equalTo(webViewHeight);
        make.bottom.equalTo(contentView).offset(0);
    }];
    
    CGFloat height = [contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - 49)];
    [self.view addSubview:scrollView];
    
    [scrollView addSubview:contentView];
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scrollView.top);
        make.left.equalTo(scrollView.left);
        make.width.equalTo(MAIN_SCREEN_WIDTH);
        make.height.equalTo(height);
    }];
    
    scrollView.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH, height);
    
}



- (void) btnClick {
    
     [self.navigationController popViewControllerAnimated:YES];
    
}



@end
